﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;

namespace DBService
{
	public partial class DlgConfig : System.Windows.Forms.Form
	{
		public DlgConfig()
		{
			this.InitializeComponent();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.server == this.cbxServer.Text.Trim() && this.userName == this.txtDbName.Text.Trim() && this.pwd == this.txtPwd.Text.Trim())
			{
				XmlUtils.WriteConfigFile(this.fileName, "Server", "value", this.cbxServer.Text.Trim());
				XmlUtils.WriteConfigFile(this.fileName, "Port", "value", this.txtPort.Text.Trim());
				XmlUtils.WriteConfigFile(this.fileName, "DbName", "value", this.txtDbName.Text.Trim());
				XmlUtils.WriteConfigFile(this.fileName, "Uid", "value", this.txtUserName.Text.Trim());
				XmlUtils.WriteConfigFile(this.fileName, "Pwd", "value", this.txtPwd.Text.Trim());
				base.Close();
				return;
			}
			MessageBox.Show("连接失败,请检查配置信息。");
		}

		private void DlgConfig_Load(object sender, EventArgs e)
		{
			if (!new FileInfo(this.fileName).Exists)
			{
				XmlUtils.CreateConfigFile(this.fileName);
			}
			this.cbxServer.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Server", "value");
			this.txtPort.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Port", "value");
			this.txtDbName.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "DbName", "value");
			this.txtUserName.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Uid", "value");
			this.txtPwd.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Pwd", "value");
		}

		private void btnTest_Click(object sender, EventArgs e)
		{
			if (BimServerDB.TestConnect(this.cbxServer.Text.Trim(), this.txtDbName.Text.Trim(), this.txtUserName.Text.Trim(), this.txtPwd.Text, "3336", false))
			{
				this.server = this.cbxServer.Text.Trim();
				this.userName = this.txtDbName.Text.Trim();
				this.pwd = this.txtPwd.Text.Trim();
				this.btnOK.Enabled = true;
				MessageBox.Show("连接成功");
			}
		}

		private void txtPort_TextChanged(object sender, EventArgs e)
		{
		}

		private void cbxServer_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void cbxServer_TextChanged(object sender, EventArgs e)
		{
			if (this.cbxServer.Text == "")
			{
				this.btnTest.Enabled = false;
				return;
			}
			this.btnTest.Enabled = true;
		}

		private string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";

		private string server = "";

		private string userName = "";

		private string pwd = "";
	}
}
